package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.Focus;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class FocusApiImpl implements FocusApi{
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void save(Focus focus) {
        mongoTemplate.save(focus);
    }

    @Override
    public void delete(Long userId, Long toUserId) {
        mongoTemplate.remove(Query.query(Criteria.where("userId").is(userId).and("toUserId").is(toUserId)),Focus.class);
    }

    @Override
    public List<Long> getByUserId(Long userId) {
        Query query=Query.query(Criteria.where("toUserId").is(userId));
        List<Focus> foci = mongoTemplate.find(query, Focus.class);
        List<Long> id = CollUtil.getFieldValues(foci, "userId", Long.class);
        return id;
    }
}
